
<?php

class monitoringCLS extends Module
{
	var $connDB = 1;
	var $func;
	var $funcDB = false;
	var $sqlFunc = 1;
	var $request = array();
	var $sysError = array();
	var $appLog = 1;

	var $table = array();
	var $type = "";
	var $totRow;
	var $analyzeArray = 1;

	function loadCampagne()
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);
		
		//carico tutti i messaggi dell'utente
		$sql = "SELECT id_campagna, cliente, campagna, descrizione, url, attiva
				FROM contaq_campagne
				WHERE 1 = 1";
			
		$sql .= " ORDER BY cliente DESC, campagna DESC";
    			
// 		print $sql;	
			 
		$rs = $this->connDB->Execute( $sql );
		
		$this->appLog->logWrite($sql);
		if ( $rs === false )
		{			
			$this->appLog->logWrite($this->connDB->ErrorMsg());
			$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			print_r ($this->sysError);
			return;
		}
		
		$this->request["idCampagnaLS"] = array();
		$this->request["clienteLS"] = array();
		$this->request["campagnaLS"] = array();
		$this->request["descrizioneLS"] = array();
		$this->request["attivaLS"] = array();
		while ( $arr = $rs->FetchRow() ) 
		{	
			$this->request["idCampagnaLS"][] = $arr["id_campagna"];
			$this->request["clienteLS"][] = $arr["cliente"];
			$this->request["campagnaLS"][] = $arr["campagna"];
			$this->request["descrizioneLS"][] = $arr["descrizione"];
			$this->request["attivaLS"][] = $arr["attiva"];
		}
		
		return true;
	}

	function loadStatoCampagna()
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);
		
		//carico tutti i messaggi dell'utente
		$sql = "SELECT * from contaq_stato_lista WHERE id_campagna=".$this->request["idCampagna"];
    			
// 		print $sql;	
			 
		$rs = $this->connDB->Execute( $sql );
		
		$this->appLog->logWrite($sql);
		if ( $rs === false )
		{			
			$this->appLog->logWrite($this->connDB->ErrorMsg());
			$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			print_r ($this->sysError);
			return;
		}
		
		$this->request["campagnaLS"] = array();
		$this->request["quantiLS"] = array();
		$this->request["statoChiamataLS"] = array();

		while ( $arr = $rs->FetchRow() ) 
		{	
			$this->request["campagnaLS"][] = $arr["campagna"];
			$this->request["quantiLS"][] = $arr["quanti"];
			$this->request["statoChiamataLS"][] = $arr["stato_chiamata"];	
		}
		
		
		return true;
	}
	
	function loadStatoGiorno()
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);
		
		//carico tutti i messaggi dell'utente
		$sql = "SELECT * from contaq_stato_lista_realtime WHERE id_campagna=".$this->request["idCampagna"];
    			
// 		print $sql;	
			 
		$rs = $this->connDB->Execute( $sql );
		
		$this->appLog->logWrite($sql);
		if ( $rs === false )
		{			
			$this->appLog->logWrite($this->connDB->ErrorMsg());
			$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			print_r ($this->sysError);
			return;
		}
		
		$this->request["campagnaLS"] = array();
		$this->request["quantiLS"] = array();
		$this->request["statoChiamataLS"] = array();
		$this->request["operatoreLS"] = array();

		while ( $arr = $rs->FetchRow() ) 
		{	
			$this->request["campagnaLS"][] = $arr["campagna"];
			$this->request["quantiLS"][] = $arr["quanti"];
			$this->request["statoChiamataLS"][] = $arr["stato_chiamata"];
			$this->request["operatoreLS"][] = $arr["operatore"];
		}
		
		
		return true;
	}

	function loadDatiCampagna()
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);
		
		$sql = "SELECT * from contaq_campagne WHERE id_campagna=".$this->request["idCampagna"];	
			 
		$rs = $this->connDB->Execute( $sql );
		
		$this->appLog->logWrite($sql);
		if ( $rs === false )
		{			
			$this->appLog->logWrite($this->connDB->ErrorMsg());
			$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			print_r ($this->sysError);
			return;
		}

		while ( $arr = $rs->FetchRow() ) 
		{	
			$this->request["minutiRiassegnazioneApp"] = $arr["minuti_riassegnazione_appuntamento"];
			$this->request["attiva"] = $arr["attiva"];
			$this->request["prefisso"] = $arr["prefix_sip"];
			$this->request["descrizione"] = $arr["descrizione"];
		}
		
		
		$sql = "SELECT * from contaq_opzioni_campagne 
			INNER JOIN contaq_esiti on codice = esito 
			WHERE id_campagna=".$this->request["idCampagna"];	
			 
		$rs = $this->connDB->Execute( $sql );
		
		$this->appLog->logWrite($sql);
		if ( $rs === false )
		{			
			$this->appLog->logWrite($this->connDB->ErrorMsg());
			$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			print_r ($this->sysError);
			return;
		}

		$this->request["idOpzioneLS"] = array();
		$this->request["esitoLS"] = array();
		$this->request["numChiamateLS"] = array();
		$this->request["minutiChiamateLS"] = array();
		$this->request["esitoNomeLS"] = array();
		while ( $arr = $rs->FetchRow() ) 
		{	
			$this->request["idOpzioneLS"][] = $arr["id_opzione_campagna"];
			$this->request["esitoLS"][] = $arr["esito"];
			$this->request["esitoNomeLS"][] = $arr["descrizione"];
			$this->request["numChiamateLS"][] = $arr["max_numero_chiamate"];
			$this->request["minutiChiamateLS"][] = $arr["numero_minuti_per_richiamare"];
		}
		
		return true;
		
	}

	function updateOzioniCampagne()
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);

		$attiva = $this->request["attiva"];
		$prefisso = $this->request["prefisso"];
		$minutiRiassegnazioneApp = $this->request["minutiRiassegnazioneApp"];
		$idCampagna = $this->request["idCampagna"];

		$sql = "SELECT fn_campagna_update($attiva,$prefisso,$minutiRiassegnazioneApp,$idCampagna) AS result";
		
		$rs = $this->connDB->Execute( $sql );
		$this->appLog->logWrite($sql);
		
		if ( $rs === false )
		{			
		      $this->appLog->logWrite($this->connDB->ErrorMsg());
		      $this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
		      //print_r ($this->sysError);
		      return;
		} 

		while ( $arr = $rs->FetchRow() ) 
		{
		    $result = $arr['result'];		   
		}
		  
		if ($result == 0 || $result == '' || $result == null || $result == false) return false;
		
		foreach($this->request["idOpzioneLS"] as $key => $val) 
		{
			$maxNumChiamate = $this->request["numChiamateLS"][$key];
			$numeroMinutiRichiamare = $this->request["minutiChiamateLS"][$key];
	
			$sql = "SELECT fn_opzioni_campagna_update($maxNumChiamate,$numeroMinutiRichiamare,$val) AS result";
			
// 			print $sql;
			
			$rs = $this->connDB->Execute( $sql );
			$this->appLog->logWrite($sql);
			
			if ( $rs === false )
			{			
			      $this->appLog->logWrite($this->connDB->ErrorMsg());
			      $this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			      //print_r ($this->sysError);
			      return;
			} 

			while ( $arr = $rs->FetchRow() ) 
			{
			    $result = $arr['result'];		   
			}
			  
			if ($result == 0 || $result == '' || $result == null || $result == false) return false;
		}
		
		
    
		return true; 
	} 

	function ricircoloNominativi()
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);

		$idCampagna = $this->request["idCampagna"];

		$sql = "SELECT fn_ricircolo_nominativi($idCampagna) AS result";
		
		$rs = $this->connDB->Execute( $sql );
		$this->appLog->logWrite($sql);
		
		if ( $rs === false )
		{			
		      $this->appLog->logWrite($this->connDB->ErrorMsg());
		      $this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
		      //print_r ($this->sysError);
		      return;
		} 

		while ( $arr = $rs->FetchRow() ) 
		{
		    $result = $arr['result'];		   
		}
		  
		if ($result == 0 || $result == '' || $result == null || $result == false) return false;
		

			
		return true; 
	} 

}
		
?>